<?php 
include_once ('../../global.php');

$mt_id = $_GET['mt_id'];
$rd_id = $_GET['rd_id'];

?>

		
			
<?php 

  $sql = "select a.mh_ho_id as sc_ho_id ,a.mh_par as sc_par, a.mh_pic,a.mh_tee_blue,a.mh_tee_red,  b.agv, b.diff, b.eagles, b.birdie, b.par, b.bogey, b.dbogey, b.worst, b.total  from match_hole a left join (
  select  mp.mp_class,round(sum(sc_score)/count(sc_score),2) as 'agv', round(sum(sc_score)/count(sc_score),2)- sc_par as 'diff' ,sc_ho_id,sc_par 
					,sum(case when sc_to_par<=-2 then 1 else 0 end ) as 'eagles'
					,sum(case when sc_to_par=-1 then 1 else 0 end ) as 'birdie'
					,sum(case when sc_to_par=0 then 1 else 0 end ) as 'par'
					,sum(case when sc_to_par=1 then 1 else 0 end ) as 'bogey'
					,sum(case when sc_to_par=2 then 1 else 0 end ) as 'dbogey'
					,sum(case when sc_to_par>=3 then 1 else 0 end ) as 'worst'
					,count(sc_score) as 'total'
					from player_score sc right join  player_summary su on sc.sc_pl_id = su.su_pl_id
					right join match_player mp on mp.mp_player_id = sc.sc_pl_id  
					where sc_mt_id = ".$mt_id." and  su_mt_id = ".$mt_id." and sc_rd_id=".$rd_id." and su_rd_id = ".$rd_id." and su_rank >0 and sc.sc_sts = ''
					and mp.mp_match = ".$mt_id." and su_rank_code not in ('WD','DQ','RTD') 
					group by  mp.mp_class, sc_ho_id 
					order by mp.mp_class,diff desc ) b 
					on a.mh_ho_id = b.sc_ho_id
					where a.mh_match = ".$mt_id." 
					order by diff
					";
    $res = $db->SelectSQL($sql);
	//var_dump ($sql); 
		$num = count($res);
		if ($num>0){
			$pos = 1;
			$diff = '';
			$rank = 1;
			$index =0;
			
			foreach ($res as $i => $n){
					  
						if($n['diff']==$diff){
							$rank  = $pos;
						}
						else{
							$diff = $n['diff'];
							$pos = $index +1;
							$rank  = $pos;
						}
						$sdata['holestatisc'][$index] = array(
							'pos'=>$rank,
							'sc_ho_id'=>$n['sc_ho_id'],
							'sc_par'=>$n['sc_par'],
							'agv'=>$n['agv']?$n['agv']:0,
							'diff'=>$n['diff']?$n['diff']:0,
							'eagles'=>$n['eagles']?$n['eagles']:0,
							'birdie'=>$n['birdie']?$n['birdie']:0,
							'par'=>$n['par']?$n['par']:0,
							'bogey'=>$n['bogey']?$n['bogey']:0,
							'dbogey'=>$n['dbogey']?$n['dbogey']:0,
							'worst'=>$n['worst']?$n['worst']:0,
							'total'=>$n['total']?$n['total']:0,
							'pic'=>$n['mh_pic']?'https://scoringchina.oss-cn-beijing.aliyuncs.com/yunnan/images/course/1048/'.$n['mh_pic']:'',
							'bluetee'=>$n['mh_tee_blue']?$n['mh_tee_blue']:0,
							'redtee'=>$n['mh_tee_red']?$n['mh_tee_red']:0,
						);
							 $sdata['holestatisc_totals']['sc_par'] += $n['sc_par'];
							 $sdata['holestatisc_totals']['agv'] += $n['agv'];
							 $sdata['holestatisc_totals']['diff'] += $n['diff'];
							 $sdata['holestatisc_totals']['eagles'] += $n['eagles'];
							 $sdata['holestatisc_totals']['birdie'] += $n['birdie'];
							 $sdata['holestatisc_totals']['par'] += $n['par'];
							 $sdata['holestatisc_totals']['bogey'] += $n['bogey'];
							 $sdata['holestatisc_totals']['dbogey'] += $n['dbogey'];
							 $sdata['holestatisc_totals']['worst'] += $n['worst'];
							 $sdata['holestatisc_totals']['total'] += $n['total'];
							 $index++;
						}
					
		}
		else {
			$sdata['holestatisc'] = array();
		}
		
		

	
	echo json_encode($sdata);
?>



